﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InstrumentCheck.Module;
using InstrumentCheck.BLL;

namespace InstrumentCheck.WebAppUI
{
    public partial class uploadAttach : BasePage
    {
        List<Attach> listAttach = new List<Attach>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FormRouting routing = Session["Routing"] as FormRouting;
                if (Request["id"] != null)
                {
                    FromCorrectQuery(routing);
                    return;
                }
                int FlowNO = routing.FlowNO;
                switch (FlowNO)
                {
                    case 0:
                    case 30:
                        pnlUpload.Visible = true;
                        break;
                    default:
                        pnlUpload.Visible = false;
                        break;
                }
                
                if (Session["AttachList"] != null)
                    listAttach = Session["AttachList"] as List<Attach>;

                if (Session["Form"] != null)
                {
                    ApplyForm form = Session["Form"] as ApplyForm;
                    listAttach = form.Item.ItemAttachment;
                }
                ViewState["listAll"] = listAttach;
                listAttach = listAttach.Where(a => a.Type == AttachmentType.Attachment).ToList<Attach>();
                ViewState["list"] = listAttach;
                BindGridViewAttach(listAttach);
            }
        }
        private void FromCorrectQuery(FormRouting routing)
        {
            pnlUpload.Visible = false;
            listAttach = new List<Attach>();
            listAttach = InstrumentCheck.BLL.CorrectQuery_bll.getAttach(Request["id"]);
            routing = new FormRouting();
            routing.FlowNO = 1;
            ViewState["routing"] = routing;
            BindGridViewAttach(listAttach);
            btnClose.Visible = false;
        }
                
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (!upload.HasFile)
                return;

            string strFileOriginalName = upload.FileName;  //原始文件名
            string strNameAll = upload.PostedFile.FileName;  //带路径的原始文件名
            string type = strFileOriginalName.Substring(strFileOriginalName.LastIndexOf('.') + 1);

            try
            {
                string strServerName = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + type;
                upload.SaveAs(PublicFunction.strAttachPath + "\\" + strServerName);

                Attach attach = new Attach();
                attach.ClientName = strFileOriginalName;
                attach.SystemName = strServerName;
                attach.Type = AttachmentType.Attachment;
                List<Attach> list = ViewState["list"] as List<Attach>;
                list.Add(attach);
                BindGridViewAttach(list);
            }
            catch
            {
                throw;
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            List<Attach> ll = ViewState["listAll"] as List<Attach>;
            ll = ll.Where(a => a.Type == AttachmentType.Picture).ToList<Attach>();
            
            //需要把上传的信息返回到父页面，并关闭当前页面
            string strAttachURL = "",strAttachInfo = "";
            List<Attach> list = ViewState["list"] as List<Attach>;
            for (int i = 0; i < list.Count; i++)
            {
                strAttachURL += "<a target=\"_blank\" href=\"" + AttachURL() + list[i].SystemName + "\">" + list[i].ClientName + "</a>;";
                strAttachInfo += list[i].SystemName + "~";
                strAttachInfo += list[i].ClientName + ";";
                ll.Add(list[i]);
            }
            if (strAttachInfo != string.Empty)
                strAttachInfo = strAttachInfo.Substring(0, strAttachInfo.Length - 1);
            if (Session["AttachList"] != null)
                Session["AttachList"] =ll;
            else
            {
                ApplyForm form = Session["Form"] as ApplyForm;
                form.Item.ItemAttachment = ll;
                Session["Form"] = form;
            }
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Attach", "Attach('" + strAttachURL + "','" + strAttachInfo + "')",true);
        }

        private void BindGridViewAttach(List<Attach> listAttach)
        {
            gvAttach.DataSource = listAttach;
            gvAttach.DataBind();

            if (listAttach == null || listAttach.Count == 0)
                gvAttach.Visible = false;
            else
                gvAttach.Visible = true;
        }

        protected void gvAttach_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[1].Visible = false;
                FormRouting routing = Session["Routing"] as FormRouting;
                if (Request["id"] != null)
                    routing = ViewState["routing"] as FormRouting;
                if (routing.FlowNO == 0|| routing.FlowNO == 30)
                    e.Row.Cells[3].Visible = true;
                else
                    e.Row.Cells[3].Visible = false;
            }

            //构造链接
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[2].Text = "<a target='_blank' href='" + AttachURL() + e.Row.Cells[1].Text + "'>" + e.Row.Cells[0].Text + "</a>";
            }
        }

        protected void gvAttach_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            List<Attach> list = ViewState["list"] as List<Attach>;
            string strSystemName = gvAttach.Rows[e.RowIndex].Cells[1].Text.Trim();
            Attach att = (from a in list
                          where a.SystemName == strSystemName
                          select a).FirstOrDefault();
            list.Remove(att);           
            BindGridViewAttach(list);
            ViewState["list"] = list;
        }

    }
}
